import { Link, useLocation, useNavigate } from 'react-router-dom'
import { useTheme } from '@/contexts/ThemeContext'
import { Moon, Sun } from 'lucide-react'
import { SettingButton } from '@/components/setting'
import { useBusiness } from '@/stores/useBusinessStore'
import { useAiCorrectorStore } from '@/stores/useAiCorrectorStore'
import { showMessage } from '@/components/common/MessageTip'
import { CorrectionStatus } from '@/types/aiCorrector'

interface NavItem {
  path: string
  label: string
}

const navItems: NavItem[] = [
  { path: '/proof-mapper', label: 'AI前期调研' },
  { path: '/work', label: '合规检查' },
  { path: '/corrector', label: 'AI智能检测' },
  { path: '/document', label: '文档管理' },
]

const Navbar = () => {
  const { theme, toggleTheme } = useTheme()
  const location = useLocation()
  const navigate = useNavigate()
  const { selectedBusiness } = useBusiness()
  const { state } = useAiCorrectorStore()
  
  // Check if current path is /business
  const isBusinessPage = location.pathname === '/business'

  // Handle business switch with AI status check
  const handleBusinessSwitch = (e: React.MouseEvent<HTMLAnchorElement>) => {
    e.preventDefault()
    
    // Check if AI correction is in progress
    if (state.status === CorrectionStatus.PROCESSING) {
      showMessage('warning', '请等待AI检测结束后再切换业务系统')
      return
    }
    
    // Navigate to business page if AI is not processing
    navigate('/business')
  }

  return (
    <nav className="sticky top-0 left-0 right-0 bg-background border-b transition-colors duration-300 z-50">
      <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
        <div className="flex items-center justify-between h-16">
          {/* Logo */}
          <div className="flex-shrink-0">
            <Link 
              to="/" 
              className="text-xl font-bold text-foreground hover:text-primary/80 transition-colors"
              aria-label="首页"
            >
              OnceOne
            </Link>
          </div>

          <div className="flex items-center space-x-6">
            {/* Navigation Items - Only show if not on business page */}
            {!isBusinessPage && (
              <div className="flex space-x-4">
                {navItems.map((item) => (
                  <Link
                    key={item.path}
                    to={item.path}
                    className="text-muted-foreground hover:text-foreground px-3 py-2 rounded-md text-sm font-medium transition-colors hover:bg-accent"
                    aria-label={item.label}
                  >
                    {item.label}
                  </Link>
                ))}
              </div>
            )}
            
            {/* Show "Switch Business" button when not on business page and a business is selected */}
            {!isBusinessPage && selectedBusiness && (
              <a
                href="/business"
                onClick={handleBusinessSwitch}
                className="text-blue-600 hover:text-blue-700 px-3 py-2 rounded-md text-sm font-medium transition-colors hover:bg-blue-50 border border-blue-200 cursor-pointer"
              >
                切换业务系统
              </a>
            )}

            {/* Settings Button - Only show if not on business page */}
            {!isBusinessPage && <SettingButton />}
            
            {/* Theme Toggle Button */}
            <button
              onClick={toggleTheme}
              className="w-11 h-11 flex items-center justify-center rounded-md hover:bg-accent transition-colors"
              aria-label={theme === 'dark' ? '切换到亮色模式' : '切换到暗色模式'}
            >
              {theme === 'dark' ? (
                <Sun className="h-5 w-5 text-foreground" />
              ) : (
                <Moon className="h-5 w-5 text-foreground" />
              )}
            </button>
          </div>
        </div>
      </div>
    </nav>
  )
}

export default Navbar 